package com.atwork.wuhua.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.BannerAdapter;
import com.atwork.wuhua.adapter.SubInAdapter;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.SubDetailBean;
import com.atwork.wuhua.bean.SubDetailNumBean;
import com.atwork.wuhua.mvp.presenter.SubDetailPresenter;
import com.atwork.wuhua.mvp.view.ISubDetailActivity;
import com.atwork.wuhua.mvp.view.ISubLaunDetailActivity;
import com.atwork.wuhua.mvp.view.ISubLaunchActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.Util;
import com.atwork.wuhua.wrideg.StarBar;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.net.IDN;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 约战详情
 */
public class SubDetailActivity extends BaseActivity implements ISubDetailActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.sb_lever)
    StarBar sbLever;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_tag1)
    TextView tvTag1;
    @BindView(R.id.tv_tag2)
    TextView tvTag2;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_action)
    TextView tvAction;
    @BindView(R.id.rv_subin)
    RecyclerView rvSubin;
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_start_name)
    TextView tvStartName;

    private SubDetailPresenter presenter;

    private SubInAdapter subInAdapter;

    private List<SubDetailNumBean.DataBean> subDetailNumBeans;


    public final static String LAUNCH = "1";//  报名约战
    public final static String DETIAL = "2";//  查看已报名约战详情

    private String id;
    private String mType;  //区分页面
    private String lat, log, tel;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_subin;
    }

    @Override
    public void initView() {
        tvTitle.setText("约战详情");

        if (getIntent().getExtras() != null) {
            id = getIntent().getExtras().getString(ConstantsMyself.INTENTID);
            mType = getIntent().getExtras().getString(ConstantsMyself.INTENTTYPE);
            if (mType.equals(LAUNCH)) {
                tvAction.setVisibility(View.VISIBLE);
            } else if (mType.equals(DETIAL)) {
                tvAction.setVisibility(View.GONE);
            }
        }
        presenter = new SubDetailPresenter();
        presenter.attachView(this);
        presenter.getData(id);
        presenter.getJoinNum(id);
        subDetailNumBeans = new ArrayList<>();


    }

    @Override
    public void setData(SubDetailBean bean) {

        tvName.setText(bean.getName());
        sbLever.setIsOnTouch(false);
        sbLever.setStarMark(Float.parseFloat(bean.getStars()));
        tvPerson.setText(bean.getJoin_count() + "/" + bean.getLimit_count());
        String discance = Util.getDistance(this, Double.parseDouble(bean.getLongitude()), Double.parseDouble(bean.getLatitude()));
        tvDistance.setText(discance);
        tvTime.setText("时间：" + bean.getStart_date() + "   " + bean.getStart_time());
        tvStartName.setText("发起人：" + bean.getCreate_member());
        tvTag1.setText(bean.getLabels().get(0));
        tvTag2.setText(bean.getLabels().get(1));
        tvAddress.setText(bean.getAddress());
        tvTag2.setText(bean.getLabels().get(1));
        log = bean.getLongitude();
        lat = bean.getLatitude();
        tel = bean.getTel();
        if (!bean.isCan_join()) {
            tvAction.setVisibility(View.GONE);
        }

        initBanner(bean.getAlbum());


    }

    @Override
    public void setJoinData(SubDetailNumBean bean) {
        subDetailNumBeans.clear();
        subDetailNumBeans.addAll(bean.getData());
        subInAdapter.setNewData(subDetailNumBeans);
    }

    @Override
    public void joinResult() {
        presenter.getData(id);
        presenter.getJoinNum(id);

    }

    private void initBanner(List<String> bannerList) {
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new BannerAdapter());
        //设置图片网址或地址的集合
        banner.setImages(bannerList);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
//        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);

        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //必须最后调用的方法，启动轮播图。
                .start();
    }


    @Override
    public void initData() {
        GridLayoutManager manager = new GridLayoutManager(this, 6);
        rvSubin.setLayoutManager(manager);
        subInAdapter = new SubInAdapter(this, R.layout.item_subin, subDetailNumBeans);
        rvSubin.setAdapter(subInAdapter);
        rvSubin.setHasFixedSize(true);
        rvSubin.setNestedScrollingEnabled(false);
    }

    @OnClick({R.id.img_left, R.id.tv_address, R.id.img_phone, R.id.tv_action})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.tv_address:
                intent = new Intent(this, MapActivity.class);
                intent.putExtra(ConstantsMyself.INTENTLAT, lat);
                intent.putExtra(ConstantsMyself.INTENTLOG, log);
                startActivity(intent);
                break;

            case R.id.img_phone:
                intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tel);
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(intent);

                break;
            case R.id.tv_action:
                presenter.postJoin(id);
                break;
        }
    }


}
